﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace TournamentOrganizerWPF
{
    [Serializable]
    public class Player
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string HomeLocation { get; set; }
        public bool HasPaid { get; set; }
        public int Wins { get; set; }
        public int ControlPoints { get; set; }
        public int Sos { get; set; }
        public List<Player> Opponents { get; set; }
        private List<Match> MyMatches { get; set; }
        public bool isActive { get; set; }

        public Player()
        {
        }

        public Player(int id, string name, string location)
        {
            Id = id;
            Name = name;
            HomeLocation = location;
            Sos = 0;
            ControlPoints = 0;
            Wins = 0;
            Opponents = new List<Player>();
            MyMatches = new List<Match>();
            isActive = true;
        }

        public override string ToString()
        {
            return Name;
        }

        public void CalculateSOS(List<Player> resultList)
        {
            for (int i = 0; i < resultList.Count; i++)
            {
                if (Opponents.Contains(resultList[i]))
                {
                    Sos += resultList.Count - i;
                }
            }
        }

        public void SetOpponent(Player player)
        {
            if (!Opponents.Contains(player))
            {
                Opponents.Add(player);
            }            
        }

        public void CalculatePoints()
        {
            Wins = 0;
            ControlPoints = 0;
            Sos = 0;
            foreach (var match in MyMatches)
            {
                if(Name.Equals(match.Player1.Name))
                {
                    ControlPoints = ControlPoints + match.ControlPointsPlayer1;
                }
                else
                {
                    ControlPoints = ControlPoints + match.ControlPointsPlayer2;
                }
                if (Name.Equals(match.WinningPlayer.Name))
                {
                    Wins++;
                }
            }
            foreach (var opponant in Opponents)
            {
                Sos = Sos + opponant.Wins;
            }
        }

        public void AddMatch(Match match)
        {
            int tempId = -1;
            if (!MyMatches.Exists(myMatch => myMatch.MatchId == match.MatchId))
            {
                MyMatches.Add(match);
            }
            else
            {
                foreach (var Match in MyMatches)
                {
                    if (Match.MatchId == match.MatchId)
                    {
                        tempId = match.MatchId;
                        
                    }                    
                }
                if (tempId > -1)
                {
                    MyMatches.Remove(MyMatches.Find(match1 => match1.MatchId == tempId));
                }                

                MyMatches.Add(match);
            }    
        }
        public bool CheckTable(int tableNumber)
        {
            bool hasPlayedOnTable = false;
            foreach (var match in MyMatches)
            {
                if (match.TableNo == tableNumber)
                {
                    hasPlayedOnTable = true;
                }
            }
            return hasPlayedOnTable;
        }

        public string StringToPrint()
        {
            return Name + ". Wins: " + Wins + ". SoS: " + Sos + ". Controlpoints: " + ControlPoints;
        }
    }
}
